package com.ruoyi.web.controller.topic;

import java.util.Date;
import java.util.List;

import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.post.domain.Post;
import com.ruoyi.topicComplain.domain.TopicComplain;
import com.ruoyi.topicComplain.service.ITopicComplainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.topic.domain.Topic;
import com.ruoyi.topic.service.ITopicService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 话题 信息操作处理
 *
 * @author ruoyi
 * @date 2019-10-12
 */
@Controller
@RequestMapping("/topic/topic")
public class TopicController extends BaseController
{
    private String prefix = "topic/topic";

	private String prefix2 = "topicComplain/topicComplain";

	@Autowired
	private ITopicService topicService;

	@Autowired
	private ITopicComplainService topicComplainService;

	@Autowired
	private IUserService userService;

	@RequiresPermissions("topic:topic:view")
	@GetMapping()
	public String topic()
	{
	    return prefix + "/topic";
	}

	/**
	 * 查询话题列表
	 */
	@RequiresPermissions("topic:topic:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Topic topic)
	{
		startPage();
        List<Topic> list = topicService.selectTopicList(topic);
		return getDataTable(list);
	}

	/**
	 * 查询待审核话题列表
	 */
	@RequiresPermissions("topic:topic:list")
	@PostMapping("/verifyList")
	@ResponseBody
	public TableDataInfo verifyList(Topic topic)
	{
		startPage();
		topic.setVerifyState("0");
		List<Topic> list = topicService.selectTopicList(topic);
		return getDataTable(list);
	}


	/**
	 * 导出话题列表
	 */
	@RequiresPermissions("topic:topic:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Topic topic)
    {
    	List<Topic> list = topicService.selectTopicList(topic);
        ExcelUtil<Topic> util = new ExcelUtil<Topic>(Topic.class);
        return util.exportExcel(list, "topic");
    }

	/**
	 * 新增话题
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 新增保存话题
	 */
	@RequiresPermissions("topic:topic:add")
	@Log(title = "话题", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Topic topic)
	{
		topic.setCreateDate(DateUtils.getNowDate());
		topic.setValid(1);
		topic.setTopicType(2);
		topic.setUserId(0);
		topic.setGlanceNum(0);
		topic.setResponseNum(0);
		topic.setVerifyState("1");
		topic.setTopicPostNum(0);
		return toAjax(topicService.insertTopic(topic));
	}

	/**
	 * 修改话题
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		Topic topic = topicService.selectTopicById(id);
		Integer userId = topic.getUserId();
		String nickName = null;
		User user = userService.selectUserById(userId);
		if (user != null) {
			nickName = user.getNickname();
		} else if (userId == 0) {
			nickName = "校园帮帮";
		}
		mmap.put("nickName", nickName);
		mmap.put("topic", topic);
		return prefix + "/edit";
	}

	/**
	 * 修改保存话题
	 */
	@RequiresPermissions("topic:topic:edit")
	@Log(title = "话题", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Topic topic)
	{
		return toAjax(topicService.updateTopic(topic));
	}

	/**
	 * 删除话题
	 */
	@RequiresPermissions("topic:topic:remove")
	@Log(title = "话题", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(topicService.deleteTopicByIds(ids));
	}

	@GetMapping("/getComplain/{id}")
	public String getComplain(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Topic topic = new Topic();
		List<Topic> topicList = topicService.selectTopicList(topic);
		mmap.put("topicList", topicList);
		mmap.put("topicId", id);
		return prefix2 + "/topicComplain";
	}

	@PostMapping("/ignoreVerify")
	@ResponseBody
	public AjaxResult ignoreVerify(Integer id) throws Exception {
//		TopicComplain topicComplain = topicComplainService.selectTopicComplainById(id);
//		topicComplain.setComplainVerify(1);
//		Integer topicId = topicComplain.getTopicId();
//		Topic persist = topicService.selectTopicById(topicId);
//		topicService.updateTopic(persist);

		Topic persist = topicService.selectTopicById(id);
		persist.setVerifyState("1");
		topicService.updateTopic(persist);
		return toAjax(topicComplainService.updateComplainVerify(1,id));
	}

	@PostMapping("/hideVerify")
	@ResponseBody
	public AjaxResult hideVerify(Integer id) throws Exception {
//		TopicComplain topicComplain = topicComplainService.selectTopicComplainById(id);
//		topicComplain.setComplainVerify(2);
//		topicComplainService.updateTopicComplain(topicComplain);
//		Integer topicId = topicComplain.getTopicId();
//		Topic persist = topicService.selectTopicById(topicId);
//		persist.setVerifyState("2");
//		return toAjax(topicService.updateTopic(persist));
		Topic persist = topicService.selectTopicById(id);
		persist.setVerifyState("2");

		//隐藏话题
		topicService.updateTopic(persist);
		//隐藏话题下帖子
		topicService.hidePostVerify(id);
		return toAjax(topicComplainService.updateComplainVerify(2,id));
	}

	@PostMapping("/ding")
	@ResponseBody
	public AjaxResult ding(Integer id) throws Exception {
//		PostComplain postComplain = postComplainService.selectPostComplainById(id);
//		postComplain.setComplainVerify("1");
//		postComplainService.updatePostComplain(postComplain);
//		Integer postId = postComplain.getPostId();
//		Post postPersist = postService.selectPostById(postId);
//		postPersist.setIsComplain(2);
//		return toAjax(postService.updatePost(postPersist));

		Topic topicPersist = topicService.selectTopicById(id);
		if (topicPersist != null) {
			Integer level = topicPersist.getTopicLevel();
			if (level != null && level == 2) {
				return AjaxResult.error("该话题已是推荐话题，请勿重复推荐！");
			} else {
				topicPersist.setTopicLevel(2);
				topicPersist.setRecommendDate(new Date());
				return toAjax(topicService.updateTopic(topicPersist));
			}
		} else {
			return AjaxResult.error("该话题不存在！");
		}
	}

}
